package com.sisyphus.mock.controllers;

import com.sisyphus.mock.data.AppDatabase;
import com.sisyphus.mock.data.PostMapper;
import com.sisyphus.mock.data.UserMapper;
import com.sisyphus.mock.data.UserPostFavoriteMapper;
import com.sisyphus.mock.data.entities.UserPostFavoriteEntity;
import com.sisyphus.mock.data.model.BaseResponse;
import com.sisyphus.mock.data.model.ListResponse;
import com.sisyphus.mock.data.entities.PostEntity;
import com.sisyphus.mock.data.entities.UserEntity;
import com.yanzhenjie.andserver.annotation.GetMapping;
import com.yanzhenjie.andserver.annotation.PostMapping;
import com.yanzhenjie.andserver.annotation.RequestHeader;
import com.yanzhenjie.andserver.annotation.RequestMapping;
import com.yanzhenjie.andserver.annotation.RequestParam;
import com.yanzhenjie.andserver.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 帖子
 */
@RestController
@RequestMapping(path = "/post")
public class PostController {

    private final UserMapper mUserMapper = AppDatabase.get().userMapper();

    private final PostMapper mPostMapper = AppDatabase.get().postMapper();

    private final UserPostFavoriteMapper mUserPostFavoriteMapper
            = AppDatabase.get().userPostFavoriteMapper();


    /**
     * 添加帖子
     */
    @PostMapping("/add")
    public BaseResponse<Object> add(
            @RequestHeader("token") String token,
            @RequestParam("content") String content
    ) {

        if (token == null) {
            return BaseResponse.fail(BaseResponse.UNAUTHORIZED_CODE, "用户未登录");
        }

        if (content == null || content.trim().isEmpty()) {
            return BaseResponse.fail("请填写没有内容");
        }

        UserEntity user = mUserMapper.findByToken(token);
        if (user == null) {
            return BaseResponse.fail(BaseResponse.UNAUTHORIZED_CODE, "token失效，请重新登录");
        }

        mPostMapper.insertPost(new PostEntity(user.id, content));

        return BaseResponse.success();
    }


    /**
     * 所有帖子列表
     */
    @GetMapping("/")
    public BaseResponse<ListResponse<List<PostEntity>>> list(
            @RequestParam("page") int page,
            @RequestParam("number") int number
    ) {

        List<PostEntity> entities = mPostMapper.find(page, number);
        return BaseResponse.success(
                new ListResponse<>(
                        null,
                        page,
                        entities.size(),
                        entities
                )
        );
    }


    /**
     * 热门帖子列表
     */
    @GetMapping("/hot")
    public BaseResponse<List<PostEntity>> hot() {
        return BaseResponse.success(mPostMapper.find(1, 3));
    }


    /**
     * 用户收藏帖子列表
     */
    @GetMapping("/user/favorite")
    public BaseResponse<ListResponse<List<PostEntity>>> postFavorite(
            @RequestHeader("token") String token,
            @RequestParam("page") int page,
            @RequestParam("number") int number
    ) {

        if (token == null) {
            return BaseResponse.fail(BaseResponse.UNAUTHORIZED_CODE, "用户未登录");
        }

        UserEntity userEntity = mUserMapper.findByToken(token);
        if (userEntity == null) {
            return BaseResponse.fail(BaseResponse.UNAUTHORIZED_CODE, "token失效，请重新登录");
        }

        // 查询指定用户收藏帖子
        List<PostEntity> postEntities = new ArrayList<>();
        List<UserPostFavoriteEntity> byUserId =
                mUserPostFavoriteMapper.findByUserId(userEntity.id, page, number);

        for (UserPostFavoriteEntity entity : byUserId) {
            postEntities.add(mPostMapper.findById(entity.postId));
        }

        return BaseResponse.success(new ListResponse<>(0, page, postEntities.size(), postEntities));
    }

}
